'use strict';

/**
 * @ngdoc directive
 * @name jgappApp.directive:FileUpload
 * @description
 * # FileUpload
 */
angular.module('jgappApp')
    .directive('fileUpload2', ['DataService', 'FileService', 'Util', 'openApi',"OpenFile", function(DataService, FileService, Util, openApi,OpenFile) {
        return {
            scope: {
                commetHandle: "=commetHandle"
            },
            template: '<div class="task-item">' +
                '<textarea rows="3" placeholder="请填写评论" ng-model="content" maxLength=200></textarea>' +
                '</div>' + '<div class="btn-wrapper cl">' +
                '<div class="btn btn-upload fl" ng-click="addpic()">{{text}}</div>' +
                '<div class="btn btn-submit fr" ng-click="submit()">提交</div>' +
                '</div>' +
                '<div class="file-list">' +
                '<div class="lh30">' +
                '<span class="c-succ" ng-click="goDetail()">{{fileData|fileName}}</span>' +
                '<span class="c-red" ng-click="delpic()" ng-show="fileData">删除附件</span></div>' +
                // '<div ng-show="src" ng-click="showImg()"><img ng-src="{{src}}" alt="" width="100%"></div>' +
                '</div>',
            restrict: 'AE',
            link: function postLink(scope, element, attrs) {
                scope.text = "上传附件";
                var cameraSuccess = function(imageData) {
                    scope.text = "上传中...";
                    FileService.upload(imageData,openApi.uploadFile).then(function(data) {
                        scope.fileData = data;
                        scope.text = "上传附件";
                    }, function(data) {
                        scope.fileData = "";
                        scope.text = "上传附件";
                    });
                }
                var cameraError = function() {};
                scope.submit = function() {
                    if (!scope.content) {
                        Util.Toast("请输入评论内容");
                        return;
                    }
                    var param = {};
                    param.content = scope.content;
                    param.attachment = scope.fileData ? scope.fileData : "";
                    scope.commetHandle(param).then(function() {
                        scope.content = "";
                        scope.fileData = "";
                    });
                };
                var choosePic = function(btn) {
                    var cameraOptions = {
                        quality: 50,
                        EncodingType: 0,
                        destinationType: 1
                    };
                    cameraOptions.sourceType = btn
                    if (btn == 0) {
                        return
                    }
                    navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions);
                }
                var delPic = function(btn) {
                    if (btn == 1) {
                        FileService.delfile(scope.fileData).then(function(data) {
                            Util.Toast(data)
                            scope.fileData = ""
                            scope.src = ""
                        });
                    }
                }
                scope.goDetail = function() {
                    if (Util.isImg(scope.fileData)) {
                        scope.src = openApi.showimg + "?fileName=" + scope.fileData;
                        Util.showFile(scope);
                    } else if (Util.isFile(scope.fileData)) {
                        OpenFile.open(fileData)
                    } else {
                        Util.Toast("移动端不支持预览")
                    }
                };
                scope.showImg = function() {
                    Util.showFile(scope);
                }
                scope.closefile = function() {
                    Util.hideFile();
                }
                scope.addpic = function() {
                    if (scope.fileData) {
                        Util.Toast("只能上传一个附件")
                        return
                    }
                    navigator.notification.confirm(
                        '附件选择方式',
                        choosePic,
                        '添加附件', ['拍照', '相册']
                    )
                }
                scope.delpic = function() {
                    navigator.notification.confirm(
                        '确定删除附件',
                        delPic,
                        '提示', ['确定', '取消']
                    )
                }

            }
        };
    }]);
